home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4507 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: "Initializer must be a constant expression"...?
  5. Date: 04 Feb 1996 17:06:59 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb4100659@qcd.lanl.gov>
  8. References: <311280AA.15FB7483@tulsa.lgc.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Glenn Carr's message of Fri, 02 Feb 1996 15:22:50 -0600
  13.  
  14. In article <311280AA.15FB7483@tulsa.lgc.com> Glenn Carr
  15. <gcarr@tulsa.lgc.com> writes:  
  16. <snip>
  17.    We recently switched compilers from gcc to xlc on AIX and we're now
  18. getting the 
  19.    following compilation warnings... 
  20.  
  21.    "../combobox.c", line 244.10: 1506-221 (E) Initializer must be a
  22. constant expression. 
  23.    "../combobox.c", line 245.10: 1506-221 (E) Initializer must be a
  24. constant expression. 
  25.    "../combobox.c", line 246.10: 1506-221 (E) Initializer must be a
  26. constant expression. 
  27.  
  28.    in the following...
  29.  
  30.        typedef struct {
  31.        char          **var;
  32.        char           *descr;
  33.        } cfgStrSpec_t;
  34.  
  35.        char           *pszDepCodeSetKey = NULL;
  36.        char           *pszDepComboBox = NULL;
  37.        char           *pszShowAllIfEmpty = NULL;
  38.        cfgStrSpec_t    pCfgStr[] =
  39.        {
  40.    244:    {&pszDepCodeSetKey, "Dependent code_set_key"},
  41.    245:    {&pszDepComboBox, "Dependent widget (combobox)"},
  42.    246:    {&pszShowAllIfEmpty, "Boolean (nonzero or 0)"},
  43.        {NULL, NULL},
  44.        };
  45.  
  46.    Is that not valid, or ANSI C?
  47.  
  48. Depends on whether or not the segment appears in global
  49. scope. Initializers for aggregate types and static duration objects
  50. need to be compile time (I use it loosely: it may include `link time')
  51. constnt expressions (the ANSI C standard never requires the compiler
  52. to build an aggregate from scratch at run time automatically. It also
  53. specifies that all static duration objects be initialized before
  54. program execution begins). Automatic variables are allocated at each
  55. invocation of the block to which they belong, and hence their address
  56. is not a compile time constant.
  57.  
  58. If the above appears in global scope it is valid. If they appear in a
  59. block, it is invalid. If there is no need for them to be automatic,
  60. but block scoping is desired, the declarations of psz... should be
  61. made `static'. 
  62.  
  63. Also beware that initializing a char* variable from a literal string
  64. is dangerous because attempt to modify a string literal leads to
  65. undefined behaviour which may not be diagnosed. It is good practice to
  66. declare the variables const char* when feasible.
  67.  
  68. Cheers
  69. Tanmoy
  70. --
  71. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  72. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  73. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  74. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  75. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  76. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  77.